為什麼 RichText 不能在 wagtail 管理員中為帖子工作?這是發生的事情的類型:<h2>嘗試 post.content|richtext</h2> (Why is RichText not working in wagtail admin for posts? This is the type of thing that happens: <h2>Trying post.content|richtext</h2>)


問題描述

為什麼 RichText 不能在 wagtail 管理員中為帖子工作?這是發生的事情的類型:

嘗試 post.content|richtext

(Why is RichText not working in wagtail admin for posts? This is the type of thing that happens:

Trying post.content|richtext

)

Wagtail 在現有 Django 站點上實現。我已經包含了最有可能包含錯誤的代碼部分。如果您還想要其他任何東西,請隨時提出要求。代碼如下:models.py

from django.conf import settings
from django.db import models
from django.utils import timezone
from django.contrib.auth.models import User
from django.urls import reverse
from django.utils.text import slugify

from wagtail.core.models import Page
from wagtail.core.fields import RichTextField
from wagtail.admin.edit_handlers import FieldPanel

class Subject(models.Model):
    subject = models.CharField(max_length=200)
    subject_slug = models.SlugField(editable=True, max_length=200, null=False, unique=True)
    def get_absolute_url(self):
        kwargs = {
            'slug': self.slug
        }
        return reverse('subject', kwargs=kwargs)
    def save(self, *args, **kwargs):
        value = self.subject
        self.slug = slugify(value, allow_unicode=True)
        super().save(*args, **kwargs)

    panels = [
        FieldPanel('subject'),
        FieldPanel('subject_slug')
    ]

    class Meta:
        # Gives the proper plural name for class called subject
        verbose_name_plural = "Subjects"

    def __str__(self):
        return self.subject

class Post(models.Model):
    author = models.ForeignKey(User, default=1, on_delete=models.SET_DEFAULT, related_name='blog_posts')
    date_added = models.DateTimeField(auto_now_add=True)
    subject = models.ForeignKey(Subject, verbose_name='Subject', default=1, on_delete=models.SET_DEFAULT)   
    title = models.CharField(max_length=200, unique=True)
    slug = models.SlugField(editable=True, max_length=200, null=False, unique=True)
    content = RichTextField()
    def get_absolute_url(self):
        kwargs = {
            'slug': self.slug
        }
        return reverse('post_detail', kwargs=kwargs)
    def save(self, *args, **kwargs):
        value = self.title
        self.slug = slugify(value, allow_unicode=True)
        super().save(*args, **kwargs)

    content_panels = Page.content_panels + [
        FieldPanel('author'),
        FieldPanel('subject'),
        FieldPanel('title'),
        FieldPanel('slug'),
        FieldPanel('content')
    ]

    class Meta:
        ordering = ['‑date_added']

    def __str__(self):
        return self.title

class Comment(models.Model):
    post = models.ForeignKey(Post, default=1, on_delete=models.SET_DEFAULT, related_name='comments')
    name = models.CharField(max_length=80)
    email = models.EmailField()
    body = models.TextField()
    made_on = models.DateTimeField(auto_now_add=True)
    active = models.BooleanField(default=False)

    class Meta:
        ordering = ['made_on']

    def __str__(self):
        return 'Comment {} by {}'.format(self.body, self.name)

index.htm

{% extends 'blog/base.html' %}

{% load wagtailcore_tags %}

{% block content %}
  {{ post.content|richtext }}
  {% for post in posts %}
    <div>
      <h2><a href="{% url 'post_detail' post.subject.subject_slug post.slug %}">{{ post.title }}</a></h2>
      <p>author: {{post.author}}, published: {{post.date_added }}</p>
      <p>{{ post.content|linebreaksbr }}</p>
    </div>
  {% endfor %}
{% endblock %}

wagtail_hooks.py

from wagtail.contrib.modeladmin.options import (
    ModelAdmin, ModelAdminGroup, modeladmin_register)

from .models import (
    Subject, Post, Comment)

class SubjectAdmin(ModelAdmin):
    model = Subject
    menu_label = 'Subjects'    from wagtail.contrib.modeladmin.options import (
    ModelAdmin, ModelAdminGroup, modeladmin_register)

from .models import (
    Subject, Post, Comment)

class SubjectAdmin(ModelAdmin):
    model = Subject
    menu_label = 'Subjects'
    menu_icon = 'pilcrow'
    list_display = ('subject', 'subject_slug')
    list_filter = ('subject', 'subject_slug')
    search_fields = ('subject', 'subject_slug')
    prepopulated_fields = {'subject_slug': ['subject'],}

class PostAdmin(ModelAdmin):
    model = Post
    menu_label = 'Post'
    menu_icon = 'author'
    list_display = ('subject', 'title', 'slug','date_added', 'author')
    list_filter = ('subject', 'date_added',)
    search_fields = ('subject', 'title', 'content', 'author')
    prepopulated_fields = {'slug': ['title'],}

modeladmin_register(SubjectAdmin)
modeladmin_register(PostAdmin)
    menu_icon = 'pilcrow'
    list_display = ('subject', 'subject_slug')
    list_filter = ('subject', 'subject_slug')
    search_fields = ('subject', 'subject_slug')
    prepopulated_fields = {'subject_slug': ['subject'],}

class PostAdmin(ModelAdmin):
    model = Post
    menu_label = 'Post'
    menu_icon = 'author'
    list_display = ('subject', 'title', 'slug','date_added', 'author')
    list_filter = ('subject', 'date_added',)
    search_fields = ('subject', 'title', 'content', 'author')
    prepopulated_fields = {'slug': ['title'],}

modeladmin_register(SubjectAdmin)
modeladmin_register(PostAdmin)

機器需要更多細節。除了我不想在我的帖子中說的以外,我不確定我還能說什麼:

Heading H2

Bold 而不是粗體

H3 粗體和斜體很有趣


參考解法

方法 1:

Thanks gasman, the way I fixed it was I implemented post.content|richtext and loaded wagtailcore_tags in templates and it works!

(by RodericRoderic)

參考文件

  1. Why is RichText not working in wagtail admin for posts? This is the type of thing that happens:

    Trying post.content|richtext

    (CC BY‑SA 2.5/3.0/4.0)

#wagtail #Python #richtext #Django






相關問題

Wagtail Cms 是否支持 Google 登錄和用戶登錄添加會話 (Does Wagtail Cms support Google login and user login add session to)

Wagtail Django-form編輯現有對象 (Wagtail Django-form edit existing object)

如何將 Wagtail 'admin' 菜單添加到自定義模板? (How to add Wagtail 'admin' menu to custom templates?)

django.db.utils.OperationalError:外鍵不匹配 - “project_projectpage”引用“auth_user” (django.db.utils.OperationalError: foreign key mismatch - "project_projectpage" referencing "auth_user")

如何使用 Wagtail 鉤子在 Wagtail 中生成自定義鏈接 (How to generate a custom link in Wagtail using Wagtail hooks)

Wagtail:如何設置單元測試以進行簡單的頁面編輯? (Wagtail: How to setup up unittest for simple page edit?)

如何修復錯誤“str”對像沒有屬性“relative_url” (How to fix error 'str' object has no attribute 'relative_url')

如何將帖子從 Wordpress 導入 Wagtail 2(Draftail 編輯器),包括圖像? (How to import posts from Wordpress to Wagtail 2 (Draftail editor) including images?)

如何用外鍵鏈接兩種形式(wagtail 形式和 django 形式)? (How to link two forms (wagtail form and django form) with a foreign key?)

為什麼 RichText 不能在 wagtail 管理員中為帖子工作?這是發生的事情的類型:<h2>嘗試 post.content|richtext</h2> (Why is RichText not working in wagtail admin for posts? This is the type of thing that happens: <h2>Trying post.content|richtext</h2>)

Windows 10 上 wagtail 的客戶端文件夾在哪裡 (Where is the client folder of wagtail on windows 10)

過濾從 Wagtail 核心頁面導入的多個模型的自定義字段 (Filter on custom field across multiple models that import from Wagtail core Page)







留言討論